<?php

/*
Name: test_case_details.php
Author: Jacob Siegel [jacob707@gmail.com]
Description: Requirement Details Page
Last Modified: 10/19/2008 by Justin Williamson
*/

require_once "includes/config.php";
require_once INCLUDE_DIR."/header.inc.php";
require_once INCLUDE_DIR."/footer.inc.php";
require_once INCLUDE_DIR."/db.inc.php";
require_once INCLUDE_DIR."/status.inc.php";

if( $_GET[ 'add_new' ] ) {
	header( "Location: test_case_details.php?action=edit&id=".nextAuto( "test_case" ) );
}

$req = new HTML_Template_IT( TEMPLATE_DIR );
if( $_GET[ 'action' ] == "edit" )
	$req->loadTemplatefile( "test_case_details_edit.tpl.html", true, true );
else
	$req->loadTemplatefile( "test_case_details_view.tpl.html", true, true );
$id = $_GET['id'];

$query = "SELECT `id`, `short`, `description` FROM `test_case` WHERE `id` = ".$id;
$query = mysql_query( $query );
$result = mysql_fetch_object( $query );
$req->setVariable( "TEST_CASE_ID", $_GET[ 'id' ] );
if( $_GET[ 'action'] == "edit" ) {
	$req->setVariable( "TEST_CASE_SHORT_DESCRIPTION", $result->short );
	$req->setVariable( "TEST_CASE_DETAILS", $result->description );
}
else {
	$req->setVariable( "TEST_CASE_SHORT_DESCRIPTION", nl2br( $result->short ) );
	$req->setVariable( "TEST_CASE_DETAILS", nl2br( $result->description ) );
}

if( $_GET[ 'action' ] == "edit" ) {
	$selected = array();
	$query = "SELECT `requirement_id` FROM `references` WHERE `test_case_id` = ".$id;
	$query = mysql_query( $query );
	while( $result = mysql_fetch_object( $query ) ) {
		array_push( $selected, $result->requirement_id );
	}
	$query = "SELECT `short`, `id` FROM `requirement` WHERE 1";
	$query = mysql_query( $query );
	while( $result = mysql_fetch_object( $query ) ) {
		$req->setVariable( "REQUIREMENT_ID", $result->id );
		$req->setVariable( "REQUIREMENT_ID_NAME", $result->id.": ".$result->short );
		if( in_array( $result->id, $selected ) ) $req->setVariable( "REQUIREMENT_IDS_SELECTED", "selected=\"true\"" );
		else $req->setVariable( "REQUIREMENT_IDS_SELECTED", "" );
		$req->parse( "requirements" );
	}
}
else {
	$query = "SELECT `requirement_id` FROM `references` WHERE `test_case_id` = ".$id;
	$query = mysql_query( $query );
	while( $result = mysql_fetch_object( $query ) ) {
		$req->setVariable( "REQUIREMENT_ID", $result->requirement_id );
		$req->parse( "requirements" );
	}
}
$header->show();
$req->show();
$footer->show();
?>
